<?php


class tWebsetup_Role extends tStandard_TreeItem
{	
	
	private $_rights;

	function permits($action, cNode $n) 
	{
		return in_array($action,$this->_rights[$n->group.'_'.$n->type]);
	}

	function getDefinition(cView $view = null)
	{
		$def = parent::getDefinition($view);
		
		if (1) 
		{
			$info = $this->cms->file->getTypes();

			//echo $def->asXML();

			$mynode = $def->xpath('//node[@group="websetup" and @name="role"]');
			$rights = $mynode[0]->addChild('group');
			$rights['group']='standard';
			$rights['field']='rights';
			$rights['physic']='true';

			foreach ($info as $group => $type) 
			{
				$grEl = $rights->addChild('group');
				$grEl['group']='standard';
				$grEl['field']=$group;
				
				foreach ($type as $t) 
				{
					$sel = $grEl->addChild('checklist');
					$sel['between']='insert:přidávat,write:zapisovat vlastní,write+:zapisovat cizí,publish:publikovat';
					$sel['field']=$group.'_'.$t;
					$sel['group']='standard';
					$sel['label']=$t;
				}
			}
			//echo $def->asXML();
			//die();
		}
		return $def;
	}
	
	function onDataLoad()
	{
		parent::onDataLoad();
		
		$r = (string)$this->data->websetup_role->rights;
		$r = unserialize($r);
		
		if (is_array($r)) 
		{
			$this->data->websetup_role->rights=null;	
			foreach ($r as $k=>$v) 
			{
				$this->data->websetup_role->rights->addChild($k,$v);
				$this->_rights[$k] = explode(',',$v);	
			}
		}
				
	}
	
	function beforeSave() 
	{
		parent::beforeSave();
		foreach ($this->data->websetup_role->rights->children() as $k=>$v) 
		{
			$r[$k]=(string)$v; 
		}
		$this->data->websetup_role->rights = serialize($r);
	}
	
}

?>